# coding=utf-8
from model.R import R
from model.log import Log
from util.logutil import write_log


def log_listen(func):
    """日志捕捉裝飾器"""

    def wrapper(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except Exception as e:
            # 写入日志信息
            log = Log()
            log.setLog(str(e), {"func": str(func), "args": str(args), "kwargs": str(kwargs)})
            write_log(log=log)
            return R.fail(None)

    return wrapper


def r_log(func, args=None, kwargs=None, message=None):
    log = Log()
    log.setLog(str(message), {"func": str(func), "args": str(args), "kwargs": str(kwargs)})
    write_log(log=log)
